package com.wanxi.servlet;

import com.wanxi.model.BrandModel;
import com.wanxi.model.BrandModelError;
import com.wanxi.service.BrandService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/add")
public class BrandAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//       1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String brandName = req.getParameter("brandName");
        String enterpriseName = req.getParameter("enterpriseName");
        String sort = req.getParameter("sort");
        String brandIntroduce = req.getParameter("brandIntroduce");
        String style = req.getParameter("style");
//       3.封装对象
//        Tool tool = new Tool();
        BrandModel model = BrandModel.builder()
                .brandName(brandName)
                .enterpriseName(enterpriseName)
                .sort(Tool.strToInt(sort))
                .brandIntroduce(brandIntroduce)
                .style(Tool.strToInt(style))
                .build();
//        model.setBrandName(brandName);
//        model.setEnterpriseName(enterpriseName);
//          4.调用服务逻辑层
        BrandService brandService = new BrandService();
//        int count = brandService.add(brandName, enterpriseName, sort, brandIntroduce, style);
        int count = brandService.add(model);
//        5.根据结果响应前端数据
        resp.sendRedirect("/findAll");
//        if (count > 0) {
//            resp.sendRedirect("/BrandFindAll.jsp");
//        } else {
//            resp.sendRedirect("/error.jsp");
//        }
//        处理如果成功以后，应该跳转到那个页面，或者将数据返回给前端
//        页面转发或者重定向
//        resp.sendRedirect("/BrandFindAll.jsp");

    }
}
